{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 各种榜单\n",
    "1. 总累计人气榜：Top20作品排名\n",
    "2. 总阅读榜：Top20作品排名\n",
    "3. 各类型累计人气榜：各类型Top20作品排名\n",
    "4. 各类型在线阅读榜：各类型Top20作品排名\n",
    "5. 作者生产力：最多产作者排行榜Top20"
   ],
   "id": "f88bf647130098e1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T07:54:53.748188Z",
     "start_time": "2025-02-26T07:54:53.495727Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from pymongo import MongoClient\n",
    "# 连接到MongoDB\n",
    "client = MongoClient('mongodb://localhost:27017/')\n",
    "# 打开learn数据库\n",
    "db = client['learn']\n",
    "# 在数据库中打开seven_cats集合\n",
    "books = db['seven_cats']"
   ],
   "id": "174d5994885c91e8",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T07:54:54.966344Z",
     "start_time": "2025-02-26T07:54:54.507595Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "# 将MongoDB数据转换为pandas数据\n",
    "add = pd.DataFrame(list(books.find({}, {'_id': 0, 'title': 1, 'type': 1, 'serialised': 1, 'popularity': 1, 'readings': 1, 'writer': 1, 'writer_word': 1})))\n",
    "add"
   ],
   "id": "e412486ecc054cdf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                   title serialised  type  readings  popularity   writer  \\\n",
       "0     重生2000：从追求青涩校花同桌开始        连载中    都市      35.5       962.0     痞子老妖   \n",
       "1         长生，苟在驿站，万年后我无敌        连载中  武侠仙侠       4.2        19.8  蚊香头家的小弟   \n",
       "2      我来自上界帝族，成婚当天媳妇跟人跑        连载中  玄幻奇幻      10.8       130.4    社恐啊社恐   \n",
       "3                  九天斩神诀        连载中  玄幻奇幻      16.4      1055.6      小知了   \n",
       "4                  鸿蒙霸体诀        连载中  玄幻奇幻      45.2       660.5      鱼初见   \n",
       "...                  ...        ...   ...       ...         ...      ...   \n",
       "7713             重生之超级战舰         完结    科幻       0.3        14.6     彩虹之门   \n",
       "7714           全球进入大洪水时代         完结    科幻       0.2        25.9     死神钓者   \n",
       "7715         分手后，我成了天眼神医        连载中    都市       0.8        16.8      风狂笑   \n",
       "7716    红楼败家子：我贾宝玉，又想纳妾了         完结   N次元       0.1        26.9    吃饭的萝卜   \n",
       "7717   火影：旅行青蛙嫌我弱，带回岩浆果实         完结   N次元       0.2        31.3      西瓜糖   \n",
       "\n",
       "      writer_word  \n",
       "0          480.48  \n",
       "1           38.60  \n",
       "2           61.96  \n",
       "3          686.74  \n",
       "4          552.87  \n",
       "...           ...  \n",
       "7713       636.23  \n",
       "7714       424.60  \n",
       "7715      3370.91  \n",
       "7716       444.69  \n",
       "7717       155.88  \n",
       "\n",
       "[7718 rows x 7 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "      <th>serialised</th>\n",
       "      <th>type</th>\n",
       "      <th>readings</th>\n",
       "      <th>popularity</th>\n",
       "      <th>writer</th>\n",
       "      <th>writer_word</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>重生2000：从追求青涩校花同桌开始</td>\n",
       "      <td>连载中</td>\n",
       "      <td>都市</td>\n",
       "      <td>35.5</td>\n",
       "      <td>962.0</td>\n",
       "      <td>痞子老妖</td>\n",
       "      <td>480.48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>长生，苟在驿站，万年后我无敌</td>\n",
       "      <td>连载中</td>\n",
       "      <td>武侠仙侠</td>\n",
       "      <td>4.2</td>\n",
       "      <td>19.8</td>\n",
       "      <td>蚊香头家的小弟</td>\n",
       "      <td>38.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>我来自上界帝族，成婚当天媳妇跟人跑</td>\n",
       "      <td>连载中</td>\n",
       "      <td>玄幻奇幻</td>\n",
       "      <td>10.8</td>\n",
       "      <td>130.4</td>\n",
       "      <td>社恐啊社恐</td>\n",
       "      <td>61.96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>九天斩神诀</td>\n",
       "      <td>连载中</td>\n",
       "      <td>玄幻奇幻</td>\n",
       "      <td>16.4</td>\n",
       "      <td>1055.6</td>\n",
       "      <td>小知了</td>\n",
       "      <td>686.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>鸿蒙霸体诀</td>\n",
       "      <td>连载中</td>\n",
       "      <td>玄幻奇幻</td>\n",
       "      <td>45.2</td>\n",
       "      <td>660.5</td>\n",
       "      <td>鱼初见</td>\n",
       "      <td>552.87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7713</th>\n",
       "      <td>重生之超级战舰</td>\n",
       "      <td>完结</td>\n",
       "      <td>科幻</td>\n",
       "      <td>0.3</td>\n",
       "      <td>14.6</td>\n",
       "      <td>彩虹之门</td>\n",
       "      <td>636.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7714</th>\n",
       "      <td>全球进入大洪水时代</td>\n",
       "      <td>完结</td>\n",
       "      <td>科幻</td>\n",
       "      <td>0.2</td>\n",
       "      <td>25.9</td>\n",
       "      <td>死神钓者</td>\n",
       "      <td>424.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7715</th>\n",
       "      <td>分手后，我成了天眼神医</td>\n",
       "      <td>连载中</td>\n",
       "      <td>都市</td>\n",
       "      <td>0.8</td>\n",
       "      <td>16.8</td>\n",
       "      <td>风狂笑</td>\n",
       "      <td>3370.91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7716</th>\n",
       "      <td>红楼败家子：我贾宝玉，又想纳妾了</td>\n",
       "      <td>完结</td>\n",
       "      <td>N次元</td>\n",
       "      <td>0.1</td>\n",
       "      <td>26.9</td>\n",
       "      <td>吃饭的萝卜</td>\n",
       "      <td>444.69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7717</th>\n",
       "      <td>火影：旅行青蛙嫌我弱，带回岩浆果实</td>\n",
       "      <td>完结</td>\n",
       "      <td>N次元</td>\n",
       "      <td>0.2</td>\n",
       "      <td>31.3</td>\n",
       "      <td>西瓜糖</td>\n",
       "      <td>155.88</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7718 rows × 7 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T07:54:56.437233Z",
     "start_time": "2025-02-26T07:54:56.354245Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 导入可视化工具\n",
    "from pyecharts import options as opts"
   ],
   "id": "601a073605191ffd",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 一二、总累计人气榜与总阅读榜比较榜：Top20作品排名",
   "id": "7661e2119c391265"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T00:39:56.468700Z",
     "start_time": "2025-02-26T00:39:56.458779Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 排序\n",
    "agg1 = pd.concat([add['title'], add['popularity']], axis=1)\n",
    "agg2 = pd.concat([add['title'], add['readings']], axis=1)\n",
    "sort1 = agg1.sort_values('popularity', ascending=False)\n",
    "sort2 = agg2.sort_values('readings', ascending=False)\n",
    "sorted1 = sort1[:20].sort_values('popularity')\n",
    "sorted2 = sort2[:20].sort_values('readings')\n",
    "sorted1"
   ],
   "id": "249ff6173cd8ce20",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "           title  popularity\n",
       "5591          捡漏      2430.8\n",
       "6212  我的七个姐姐绝色倾城      2453.1\n",
       "56        民间诡闻实录      2517.5\n",
       "4704          元尊      2604.1\n",
       "4607     史上最强炼气期      2606.8\n",
       "7043        都市潜龙      2884.1\n",
       "51          寒门枭士      3089.4\n",
       "4557       剑道第一仙      3279.3\n",
       "4815         龙王令      3545.2\n",
       "134         绝世强龙      3595.8\n",
       "4613        无敌剑域      3825.6\n",
       "25         无敌六皇子      4086.3\n",
       "20          盖世神医      4444.0\n",
       "4629      史上最狂老祖      4526.1\n",
       "4594       都市古仙医      4594.0\n",
       "4555          剑来      4687.6\n",
       "4645       雪中悍刀行      5065.5\n",
       "4569       太荒吞天诀      5659.7\n",
       "4558        我有一剑      7377.8\n",
       "4562        一剑独尊     10058.2"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "      <th>popularity</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5591</th>\n",
       "      <td>捡漏</td>\n",
       "      <td>2430.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6212</th>\n",
       "      <td>我的七个姐姐绝色倾城</td>\n",
       "      <td>2453.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56</th>\n",
       "      <td>民间诡闻实录</td>\n",
       "      <td>2517.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4704</th>\n",
       "      <td>元尊</td>\n",
       "      <td>2604.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4607</th>\n",
       "      <td>史上最强炼气期</td>\n",
       "      <td>2606.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7043</th>\n",
       "      <td>都市潜龙</td>\n",
       "      <td>2884.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>寒门枭士</td>\n",
       "      <td>3089.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4557</th>\n",
       "      <td>剑道第一仙</td>\n",
       "      <td>3279.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4815</th>\n",
       "      <td>龙王令</td>\n",
       "      <td>3545.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>134</th>\n",
       "      <td>绝世强龙</td>\n",
       "      <td>3595.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4613</th>\n",
       "      <td>无敌剑域</td>\n",
       "      <td>3825.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>无敌六皇子</td>\n",
       "      <td>4086.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>盖世神医</td>\n",
       "      <td>4444.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4629</th>\n",
       "      <td>史上最狂老祖</td>\n",
       "      <td>4526.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4594</th>\n",
       "      <td>都市古仙医</td>\n",
       "      <td>4594.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4555</th>\n",
       "      <td>剑来</td>\n",
       "      <td>4687.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4645</th>\n",
       "      <td>雪中悍刀行</td>\n",
       "      <td>5065.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4569</th>\n",
       "      <td>太荒吞天诀</td>\n",
       "      <td>5659.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4558</th>\n",
       "      <td>我有一剑</td>\n",
       "      <td>7377.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4562</th>\n",
       "      <td>一剑独尊</td>\n",
       "      <td>10058.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T00:39:57.095416Z",
     "start_time": "2025-02-26T00:39:57.091204Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 重复率\n",
    "repeat = len(set(sorted1['title']) & set(sorted2['title'])) / 20\n",
    "repeat"
   ],
   "id": "c810199f527ca9d9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T00:39:57.675566Z",
     "start_time": "2025-02-26T00:39:57.671975Z"
    }
   },
   "cell_type": "code",
   "source": "from pyecharts.charts import Bar, Liquid, Grid",
   "id": "aa0c6fbc04e7460c",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T00:39:58.500374Z",
     "start_time": "2025-02-26T00:39:58.492785Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 总累计人气榜Top15\n",
    "bar1 = Bar()\n",
    "bar1.add_xaxis(sorted1['title'].tolist())\n",
    "bar1.add_yaxis('', sorted1['popularity'].tolist(), label_opts=opts.LabelOpts(is_show=True, position='right'))\n",
    "bar1.reversal_axis()\n",
    "bar1.set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title=\"总累计人气榜Top20\", pos_left=\"25%\", pos_top=\"2%\"),\n",
    "        # axislabel_opts是否显示x轴数值，splitline_opts是否展示轴线\n",
    "        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(is_show=False), splitline_opts=opts.SplitLineOpts(is_show=False)),\n",
    "        yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=False))\n",
    ")"
   ],
   "id": "a05e427cf2f7797b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pyecharts.charts.basic_charts.bar.Bar at 0x260a40c6b10>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T00:40:39.636463Z",
     "start_time": "2025-02-26T00:40:39.628244Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 总阅读频次榜Top15\n",
    "bar2 = Bar()\n",
    "bar2.add_xaxis(sorted2['title'].tolist())\n",
    "bar2.add_yaxis('', sorted2['readings'].tolist(), label_opts=opts.LabelOpts(is_show=True, position='left'))\n",
    "bar2.reversal_axis()\n",
    "bar2.set_global_opts(\n",
    "        title_opts=opts.TitleOpts(title=\"总阅读频次榜Top20\", pos_right=\"25%\", pos_top=\"2%\"),\n",
    "        xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(is_show=False), is_inverse=True, splitline_opts=opts.SplitLineOpts(is_show=False)),      # is_inverse反转输出方向\n",
    "        yaxis_opts=opts.AxisOpts(position=\"right\", splitline_opts=opts.SplitLineOpts(is_show=False))\n",
    "        # position=\"right\"将y轴默认左侧显示变为右侧\n",
    ")"
   ],
   "id": "18b2dc644fafbeb6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pyecharts.charts.basic_charts.bar.Bar at 0x260a40c5590>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T00:40:42.639875Z",
     "start_time": "2025-02-26T00:40:42.634155Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 构建水球图\n",
    "liquid = Liquid()\n",
    "# 列表长度表示波纹数量（取最小波纹显示），is_outline_show=False不显示边框\n",
    "liquid.add(\"重复率\", [repeat, repeat, repeat], is_outline_show=False)\n",
    "liquid.set_global_opts(title_opts=opts.TitleOpts(title=\"人气榜与阅读榜的书籍重复率\", pos_left=\"center\", pos_bottom=\"15%\"))"
   ],
   "id": "6ae6ff2e74f35c1f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pyecharts.charts.basic_charts.liquid.Liquid at 0x2608364f5d0>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T00:40:43.170253Z",
     "start_time": "2025-02-26T00:40:43.144116Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将三张图片进行整合\n",
    "grid = Grid(init_opts=opts.InitOpts(width=\"1260px\", height=\"720px\"))\n",
    "grid.add(bar1, grid_opts=opts.GridOpts(pos_right=\"52%\", pos_left=\"16%\"))\n",
    "grid.add(bar2, grid_opts=opts.GridOpts(pos_left=\"52%\", pos_right=\"16%\"))\n",
    "grid.add(liquid, grid_opts=opts.GridOpts())\n",
    "grid.render(\"D:\\sevencats-novels-analysis\\images\\七猫人气and阅读Top20.html\")"
   ],
   "id": "dc02882a624f3baf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\sevencats-novels-analysis\\\\images\\\\七猫人气and阅读Top20.html'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 三、各类型累计人气榜：各类型Top20作品排名",
   "id": "a34cc908a20c5227"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T01:16:09.796412Z",
     "start_time": "2025-02-26T01:16:09.785784Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 排序\n",
    "agg = pd.concat([add['title'], add['type'], add['popularity']], axis=1)\n",
    "grouped = agg.groupby('type')\n",
    "types = grouped.sum('popularity').sort_values('popularity', ascending=False)\n",
    "types.index"
   ],
   "id": "6f9ae0f6512878d9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['都市', '玄幻奇幻', '历史', '武侠仙侠', '科幻', 'N次元', '奇闻异事', '游戏', '体育', '军事',\n",
       "       '现实题材'],\n",
       "      dtype='object', name='type')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T01:16:11.267718Z",
     "start_time": "2025-02-26T01:16:11.263205Z"
    }
   },
   "cell_type": "code",
   "source": "from pyecharts.charts import Bar, Timeline",
   "id": "a60a2eb596323498",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T01:16:41.095317Z",
     "start_time": "2025-02-26T01:16:41.066666Z"
    }
   },
   "cell_type": "code",
   "source": [
    "tl = Timeline(init_opts=opts.InitOpts(width=\"1260px\", height=\"720px\"))\n",
    "for t in types.index:\n",
    "        sort = grouped.get_group(t).sort_values('popularity', ascending=False)\n",
    "        sort20 = sort[:20].sort_values('popularity')\n",
    "        bar = Bar()\n",
    "        bar.add_xaxis(sort20['title'].tolist())\n",
    "        bar.add_yaxis('', sort20['popularity'].tolist(), label_opts=opts.LabelOpts(position=\"right\"))\n",
    "        bar.reversal_axis()\n",
    "        bar.set_global_opts(\n",
    "                title_opts=opts.TitleOpts(title=f\"{t}累计人气榜\", pos_left=\"center\", pos_top=\"2%\"), \n",
    "                xaxis_opts=opts.AxisOpts(name=\"累计人气值（万）\"),\n",
    "        )\n",
    "        tl.add(bar, f\"{t}\")\n",
    "        tl.add_schema(\n",
    "                is_auto_play=True,     # 开启自动播放\n",
    "                play_interval=2000,    # 轮播间隔（毫秒，2秒）\n",
    "                is_loop_play=True,     # 循环播放\n",
    "                is_timeline_show=True, # 显示时间轴\n",
    "        )\n",
    "tl.render(\"D:\\sevencats-novels-analysis\\images\\七猫各类型累计人气榜.html\")"
   ],
   "id": "4920c04fa4485ffb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\sevencats-novels-analysis\\\\images\\\\七猫各类型累计人气榜.html'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 四、各类型在线阅读榜：各类型Top20作品排名",
   "id": "bba5c04312dada3d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T01:17:34.916749Z",
     "start_time": "2025-02-26T01:17:34.907245Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 排序\n",
    "agg = pd.concat([add['title'], add['type'], add['readings']], axis=1)\n",
    "grouped = agg.groupby('type')\n",
    "types = grouped.sum('readings').sort_values('readings', ascending=False)\n",
    "types.index"
   ],
   "id": "64cabf4856249635",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['都市', '玄幻奇幻', '历史', '武侠仙侠', '奇闻异事', '科幻', 'N次元', '游戏', '军事', '体育',\n",
       "       '现实题材'],\n",
       "      dtype='object', name='type')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T01:17:35.642866Z",
     "start_time": "2025-02-26T01:17:35.638101Z"
    }
   },
   "cell_type": "code",
   "source": "from pyecharts.charts import Bar, Timeline",
   "id": "86a825854d8822e6",
   "outputs": [],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T01:17:36.433645Z",
     "start_time": "2025-02-26T01:17:36.413756Z"
    }
   },
   "cell_type": "code",
   "source": [
    "tl = Timeline(init_opts=opts.InitOpts(width=\"1260px\", height=\"720px\"))\n",
    "for t in types.index:\n",
    "        sort = grouped.get_group(t).sort_values('readings', ascending=False)\n",
    "        sort20 = sort[:20].sort_values('readings')\n",
    "        bar = Bar()\n",
    "        bar.add_xaxis(sort20['title'].tolist())\n",
    "        bar.add_yaxis('', sort20['readings'].tolist(), label_opts=opts.LabelOpts(position=\"right\"))\n",
    "        bar.reversal_axis()\n",
    "        bar.set_global_opts(\n",
    "                title_opts=opts.TitleOpts(title=f\"{t}在线阅读榜\", pos_left=\"center\", pos_top=\"2%\"), \n",
    "                xaxis_opts=opts.AxisOpts(name=\"阅读频次（万）\"),\n",
    "        )\n",
    "        tl.add(bar, f\"{t}\")\n",
    "        tl.add_schema(\n",
    "                is_auto_play=True,     # 开启自动播放\n",
    "                play_interval=2000,    # 轮播间隔（毫秒，2秒）\n",
    "                is_loop_play=True,     # 循环播放\n",
    "                is_timeline_show=True, # 显示时间轴\n",
    "        )\n",
    "tl.render(\"D:\\sevencats-novels-analysis\\images\\七猫各类型在线阅读榜.html\")"
   ],
   "id": "75cd09ad741f348a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\sevencats-novels-analysis\\\\images\\\\七猫各类型在线阅读榜.html'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 五、作者生产力：最多产作者排行榜Top20",
   "id": "27787fa64da56181"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T07:55:02.446470Z",
     "start_time": "2025-02-26T07:55:02.436877Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 分组排序\n",
    "agg = pd.concat([add['writer'], add['writer_word']], axis=1)\n",
    "set_data = agg.drop_duplicates()\n",
    "sort = set_data.sort_values('writer_word', ascending=False)[:20].round(2)\n",
    "sort"
   ],
   "id": "890368199afbe908",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      writer  writer_word\n",
       "7344      明宇      5103.03\n",
       "4862     风青阳      4394.41\n",
       "5378     失落叶      4057.28\n",
       "6593    莫若梦兮      4014.75\n",
       "4785    鱼人二代      3986.31\n",
       "6508    执笔天涯      3392.92\n",
       "5434    半块铜板      3389.73\n",
       "4998     风狂笑      3370.91\n",
       "6308   新版红双喜      3257.69\n",
       "4708      莫默      3223.34\n",
       "4674    风凌天下      3216.07\n",
       "5369    十月流年      3175.89\n",
       "5066  上山打老虎额      3075.92\n",
       "6051    雨暮浮屠      2892.31\n",
       "5289    一叶青天      2882.97\n",
       "7520    千里牧尘      2829.74\n",
       "4557     萧瑾瑜      2821.25\n",
       "4714    厌笔萧生      2766.88\n",
       "4569    铁马飞桥      2683.86\n",
       "4960     夜行月      2679.40"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>writer</th>\n",
       "      <th>writer_word</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>7344</th>\n",
       "      <td>明宇</td>\n",
       "      <td>5103.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4862</th>\n",
       "      <td>风青阳</td>\n",
       "      <td>4394.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5378</th>\n",
       "      <td>失落叶</td>\n",
       "      <td>4057.28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6593</th>\n",
       "      <td>莫若梦兮</td>\n",
       "      <td>4014.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4785</th>\n",
       "      <td>鱼人二代</td>\n",
       "      <td>3986.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6508</th>\n",
       "      <td>执笔天涯</td>\n",
       "      <td>3392.92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5434</th>\n",
       "      <td>半块铜板</td>\n",
       "      <td>3389.73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4998</th>\n",
       "      <td>风狂笑</td>\n",
       "      <td>3370.91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6308</th>\n",
       "      <td>新版红双喜</td>\n",
       "      <td>3257.69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4708</th>\n",
       "      <td>莫默</td>\n",
       "      <td>3223.34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4674</th>\n",
       "      <td>风凌天下</td>\n",
       "      <td>3216.07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5369</th>\n",
       "      <td>十月流年</td>\n",
       "      <td>3175.89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5066</th>\n",
       "      <td>上山打老虎额</td>\n",
       "      <td>3075.92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6051</th>\n",
       "      <td>雨暮浮屠</td>\n",
       "      <td>2892.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5289</th>\n",
       "      <td>一叶青天</td>\n",
       "      <td>2882.97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7520</th>\n",
       "      <td>千里牧尘</td>\n",
       "      <td>2829.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4557</th>\n",
       "      <td>萧瑾瑜</td>\n",
       "      <td>2821.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4714</th>\n",
       "      <td>厌笔萧生</td>\n",
       "      <td>2766.88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4569</th>\n",
       "      <td>铁马飞桥</td>\n",
       "      <td>2683.86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4960</th>\n",
       "      <td>夜行月</td>\n",
       "      <td>2679.40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T07:55:04.406428Z",
     "start_time": "2025-02-26T07:55:04.401663Z"
    }
   },
   "cell_type": "code",
   "source": "from pyecharts.charts import Funnel",
   "id": "a058acd2b21a041f",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T07:55:05.117537Z",
     "start_time": "2025-02-26T07:55:05.089207Z"
    }
   },
   "cell_type": "code",
   "source": [
    "funnel = Funnel(init_opts=opts.InitOpts(width=\"720px\", height=\"720px\"))\n",
    "funnel.add(\"\", [list(z) for z in zip(sort['writer'], sort['writer_word'])], label_opts=opts.LabelOpts(position=\"right\", formatter=\"{b}：{c}万字\"), min_=2500, max_=5200, funnel_align=\"left\")\n",
    "# legend_opts=opts.LegendOpts(is_show=False)隐藏图例\n",
    "funnel.set_global_opts(legend_opts=opts.LegendOpts(is_show=False), title_opts=opts.TitleOpts(title=\"最多产作者排行榜Top20\", pos_left=\"12%\", pos_top=\"3%\"))\n",
    "funnel.render(\"D:\\sevencats-novels-analysis\\images\\最多产作者排行榜.html\")"
   ],
   "id": "250d13e661ee219b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\sevencats-novels-analysis\\\\images\\\\最多产作者排行榜.html'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
